package admin

import (
	"booking/common/handler"
	"booking/common/validator"
	"booking/models"

	"github.com/gin-gonic/gin"
)

func ChangeStatus(c *gin.Context) {
	var params struct {
		Id uint `json:"id" binding:"required,gt=0"`
	}
	if err := c.ShouldBindJSON(&params); err != nil {
		handler.Error(validator.Translate(err), c)
		return
	}
	var userinfo models.Admin
	models.Db.Model(models.Admin{}).
		Where("id = ?", params.Id).
		First(&userinfo)
	if userinfo.Id <= 0 {
		handler.Error("用户不存在", c)
		return
	}
	err := models.Db.
		Model(models.Admin{}).
		Where("id = ?", userinfo.Id).
		Update("status", 1^userinfo.Status).Error
	if err != nil {
		handler.Error(err.Error(), c)
		return
	}
	handler.Success(nil, c)
}
